
// 完整函数类型
// let 名称:(参数类型)=>返回值 = function(参数类型):返回值{方法体}
let myAdd: (x: number, y: number) => number =
    function (x: number, y: number): number {
        return x + y;
    };

// 参数类型 返回值类型 可选参数 剩余参数
function f11(a: object, x: number = 0, y: string = "无", z: boolean = false, k?: number,...params:string[]): void {
    console.log(x, y, z, k, params)
}

f11({a: 123}, 2)
f11({a: 123}, 2,"有",true,30,"剩余1","剩余2")




// 迭代器
let Iterator = (arr:any[])=> {
    let n:number = 0;
    return {
        next: ():any => arr[n++]
    };
}
let iterator = Iterator([1,3,2,6,4])
console.log(iterator.next())
console.log(iterator.next())
console.log(iterator.next())



interface tp{
    suit:string,
    card:number
}
// this
let deck1 = {
    suits: ["hearts", "spades", "clubs", "diamonds"],
    cards: [2,3],
    createCardPicker: function():{suit:string,card:number} {
        // 在这里 this.suits[0]的类型依旧为any
        return {suit: this.suits[0], card: this.cards[1]};
    }
}
let cardPicker1 = deck1.createCardPicker();
console.log("card: " + cardPicker1.card + " of " + cardPicker1.suit);

// this参数
interface Card2 {
    suit: string;
    card: number;
}
interface Deck {
    suits: string[];
    cards: number[];
    createCardPicker(this: Deck): () => Card2;
}
let deck: Deck = {
    suits: ["hearts", "spades", "clubs", "diamonds"],
    cards: Array(52),
    createCardPicker: function(this: Deck) {
        return () => {
            let pickedCard = Math.floor(Math.random() * 52);
            let pickedSuit = Math.floor(pickedCard / 13);

            return {suit: this.suits[pickedSuit], card: pickedCard % 13};
        }
    }
}

let cardPicker = deck.createCardPicker();
let pickedCard = cardPicker();

console.log("card: " + pickedCard.card + " of " + pickedCard.suit);




// 重载
function fnn11(name:string):string;
function fnn11(name:number,str:string):number;
function fnn11(val:any,...arg:any[]):any {
    console.log(val,arg)
    if (typeof val === "string"){
        return "string"
    } else if (typeof val === "number"){
        return "number"
    }
}

fnn11("零三")
fnn11(12,"21")






















